﻿
#include "Utils.h"

Utils::Utils(QObject *parent)
    : QObject{parent}
{

}

QByteArray Utils::quint16ToByteArray(quint16 value)
{
    QByteArray byteArray;
    byteArray.append(static_cast<char>((value >> 8) & 0xFF));
    byteArray.append(static_cast<char>(value & 0xFF));
    return byteArray;
}

QByteArray Utils::quint16VecToByteArray(const QVector<quint16> &vector) {
    QByteArray byteArray;
    for (const quint16 &value : vector) {
        byteArray.append(static_cast<char>((value >> 8) & 0xFF));
        byteArray.append(static_cast<char>(value & 0xFF));
    }
    return byteArray;
}

quint16 Utils::bytesToQuint16(char high,char low,QSysInfo::Endian endian)
{
    quint16 result = 0;
    if (endian  == QSysInfo::BigEndian) {
        result = (static_cast<quint16>(high) << 8) | static_cast<quint8>(low);
    } else {
        result = (static_cast<quint16>(low) << 8) | static_cast<quint8>(high);
    }
    return result;
}



